package cn.xorange.commons.utils.lang;

import cn.hutool.core.text.NamingCase;
import cn.hutool.core.text.StrFormatter;

import java.util.Objects;

import static cn.hutool.core.text.CharSequenceUtil.subSuf;

/**
 * @author : yangjian
 * @date : 2023/11/22
 * @description :
 */
public class StringUtils{

    public static boolean isEmpty(String str){
        if(Objects.nonNull(str)){
            return str.isEmpty();
        }else {
            return true;
        }
    }

    public static String format(String template, Object... argArray){
        return StrFormatter.formatWith(template, "{}", argArray);
    }

    public static boolean contains(String str,String... containsStr){
        if (!Objects.isNull(str) && !Objects.isNull(containsStr)) {
            for (String cStr : containsStr) {
                if(str.contains(cStr)){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean containsIgnoreCase(String str,String... containsStr){
        if (!Objects.isNull(str) && !Objects.isNull(containsStr)) {
            for (String cStr : containsStr) {
                if(str.toLowerCase().contains(cStr.toLowerCase())){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean equals(String str,String... containsStr){
        if (!Objects.isNull(str) && !Objects.isNull(containsStr)) {
            for (String cStr : containsStr) {
                if(str.equals(cStr)){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 转驼峰
     * @return
     * 		camelCase("hello_world") == "helloWorld"
     * 		capCamelCase("hello_world") == "HelloWorld"
     * 		uncamelCase("helloWorld") = "hello_world"
     */
    public static String toCamelCase(String str) {
        return NamingCase.toCamelCase(str);
    }

    /**
     * 驼峰转下划线
     */
    public static String toUnderlineCase(String str) {
        return NamingCase.toUnderlineCase(str);
    }

    public static String upperFirst(CharSequence str) {
        if (null == str) {
            return null;
        } else {
            if (str.length() > 0) {
                char firstChar = str.charAt(0);
                if (Character.isLowerCase(firstChar)) {
                    return Character.toUpperCase(firstChar) + subSuf(str, 1);
                }
            }
            return str.toString();
        }
    }

    public static String replaceEach(String text, String[] searchList, String[] replacementList) {
        return org.apache.commons.lang3.StringUtils.replaceEach(text, searchList, replacementList);
    }

    public static String[] toArray(String str, String split){
        if(Objects.isNull(str) || str.isEmpty()){
            return new String[] {};
        }else {
            return str.split(split);
        }
    }

    public static Long[] toLongArray(String str, String split){
        if (StringUtils.isEmpty(str))
        {
            return new Long[] {};
        }
        String[] arr = str.split(split);
        final Long[] longs = new Long[arr.length];
        for (int i = 0; i < arr.length; i++)
        {
            final Long v = ObjectUtils.toLong(arr[i]);
            longs[i] = v;
        }
        return longs;
    }

}
